package com.yunchao.bank.splash

import android.annotation.SuppressLint
import android.content.Intent
import android.view.KeyEvent
import androidx.lifecycle.lifecycleScope
import com.xuexiang.xui.utils.KeyboardUtils
import com.xuexiang.xui.widget.activity.BaseSplashActivity
import com.yunchao.bank.configure.presentation.screen.ConfigureActivity
import com.yunchao.bank.main.presentation.screen.MainActivity
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel


@SuppressLint("CustomSplashScreen")
class SplashActivity : BaseSplashActivity() {

    private val viewModel: SplashViewModel by viewModel<SplashViewModel>()

    override fun onCreateActivity() {
        startSplash(true)

        lifecycleScope.launch { viewModel.uiEffectFlow.collect(::sideEffect) }
    }

    override fun onSplashFinished() {
        viewModel.getConfigure()
    }

    private fun sideEffect(uiEffect: SplashViewModel.UiEffect) {
        when (uiEffect) {
            is SplashViewModel.UiEffect.NavigateToMain -> startActivity(
                Intent(this@SplashActivity, MainActivity::class.java)
            )

            is SplashViewModel.UiEffect.NavigateToConfig -> ConfigureActivity.openConfigure(this@SplashActivity)
        }
        finish()
    }

    /**
     * 菜单、返回键响应
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return KeyboardUtils.onDisableBackKeyDown(keyCode) && super.onKeyDown(keyCode, event)
    }
}